import re
from IPy import IP


# 192.168.1.* --->192.168.1.1到192.168.1.255
def __ip_split_four_star(ip):
    ip_1 = ip.split('.')[-4]
    ip_2 = ip.split('.')[-3]
    ip_3 = ip.split('.')[-2]
    ip_4 = ip.split('.')[-1]
    ip_to = []
    for i in range(1, 256):
        ip_result = ip_1 + '.' + ip_2 + '.' + ip_3 + '.' + str(i)
        ip_to.append(ip_result)
    return ip_to


# 192.168.1.0-255
def __ip_split_four_line(ip):
    dic = []
    ip_1 = ip.split('-')[-2].split('.')[-4]
    ip_2 = ip.split('-')[-2].split('.')[-3]
    ip_3 = ip.split('-')[-2].split('.')[-2]
    ip_4 = ip.split('-')[-2].split('.')[-1]
    ip_last = ip.split('-')[-1]
    ip_last.strip()
    ip_len = int(ip_last) - int(ip_4) + 1
    for i in range(ip_len):
        ip_last = int(ip_4) + i
        ip_result = ip_1 + '.' + ip_2 + '.' + ip_3 + '.' + str(ip_last)
        dic.append(ip_result)
    return dic


# 192.168.1-10.3
def __ip_split_three_line(ip):
    ip_1 = ip.split('-')[-2].split('.')[-3]
    ip_2 = ip.split('-')[-2].split('.')[-2]
    ip_3 = ip.split('-')[-2].split('.')[-1]  # 1
    ip_last = ip.split('-')[-1]  # 10.3
    ip_last_1 = ip_last.split('.')[-2]  # 10
    ip_last_2 = ip_last.split('.')[-1]  # 3
    ip_3p = []
    for i in range(int(ip_3), int(ip_last_1) + 1):
        ip_3p.append(ip_1 + '.' + ip_2 + '.' + str(i) + '.' + ip_last_2)
    return ip_3p


# 192.168.1-10.*
def __ip_split_three_line_four_star(ip):
    ip_1 = ip.split('-')[-2].split('.')[-3]
    ip_2 = ip.split('-')[-2].split('.')[-2]
    ip_3 = ip.split('-')[-2].split('.')[-1]  # 1
    ip_last = ip.split('-')[-1]  # 10
    ip_last_1 = ip_last.split('.')[-2]
    ip_3p = []
    for i in range(int(ip_3), int(ip_last_1) + 1):
        for j in range(1, 256):
            ip_3p.append(ip_1 + '.' + ip_2 + '.' + str(i) + '.' + str(j))
    return ip_3p


# 192.168.1.0/24
def __ip_split_mask(ip):
    dic = []
    ip = IP(ip)
    for x in ip:
        dic.append(str(x))
    return dic


# 192.168.1.250-192.168.2.5
def __ip_split_blocks(ip):
    ipx = ip.split('-')
    ip2num = lambda x: sum([256 ** i * int(j) for i, j in enumerate(x.split('.')[::-1])])
    num2ip = lambda x: '.'.join([str(x // (256 ** i) % 256) for i in range(3, -1, -1)])
    a = [num2ip(i) for i in range(ip2num(ipx[0]), ip2num(ipx[1]) + 1) if not ((i + 1) % 256 == 0 or (i) % 256 == 0)]
    return a


# 原样返回
def __ip(ip):
    return [ip]


__method_dict = {
    "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$": __ip,
    "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}\*$": __ip_split_four_star,
    "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)-(2[0-4]\d|25[0-5]|[01]?\d\d?)$": __ip_split_four_line,
    "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}(2[0-4]\d|25[0-5]|[01]?\d\d?)-(2[0-4]\d|25[0-5]|[01]?\d\d?)\.(2[0-4]\d|25[0-5]|[01]?\d\d?)$": __ip_split_three_line,
    "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}(2[0-4]\d|25[0-5]|[01]?\d\d?)-(2[0-4]\d|25[0-5]|[01]?\d\d?)\.\*$": __ip_split_three_line_four_star,
    "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)/[1-9]\d?$": __ip_split_mask,
    "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)-((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$": __ip_split_blocks
}


# 解析单个ip
def __ip_split(ip):
    ans = []
    for key in __method_dict.keys():
        obj = re.match(key, ip)
        if obj:
            method = __method_dict.get(key)
            ans = method(ip)
            break
    return ans


# ips解析
def ips_split(ips):
    dic = []
    for ip in ips.split(","):
        ip = ip.strip()
        for x in __ip_split(ip):
            dic.append(x)
    return list(set(dic))  # 目的是为了去重


# 校验ips
def validate_ips(ips):
    for ip in ips.split(","):
        ip = ip.strip()
        if not bool(__ip_split(ip)):
            # 有一个没值则为出错
            return False
    return True


if __name__ == '__main__':
    ips = "192.168.1.213, 192.168.2.*, 192.168.3-4.3, 192.168.5-6.*, 192.168.7.0/24, 192.168.8.1-192.168.9.2"
    print(ips_split(ips))
